package com.cardbaobao.cardbabyclient.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.callback.RecycleItemClickedCallback;
import com.cardbaobao.cardbabyclient.model.RaidersList;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import java.util.List;

/**
 * Created by Jiangzx on 2017/6/26 14:34.
 * 办卡攻略adapter
 */

public class RaidersAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_NO_DATA=1;//无数据
    public static final int TYPE_WOOL_RAIDERS=2;//羊毛攻略
    public static final int TYPE_CREDIT_RAIDERS=4;//办卡攻略
    public static final int TYPE_UPPER_QUOTA_RAIDERS=5;//提额攻略
    public static final int TYPE_TOPIC_RAIDERS=6;//专题攻略
    private List<RaidersList.Raiders> data;
    private int raidersType;//0、1、3精选攻略布局， 2用羊毛布局
    public void setRaidersType(int raidersType){
        this.raidersType=raidersType;
    }
    public int getRaidersType(){
        return raidersType;
    }
    private RecycleItemClickedCallback clickedCallback;
    public void setRecycleItemClickedCallback(RecycleItemClickedCallback clickedCallback){
        this.clickedCallback=clickedCallback;
    }
    public RaidersAdapter(List<RaidersList.Raiders> data) {
        this.data=data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        if(viewType==TYPE_NO_DATA){
            //无数据
            return new NoDataHolder(inflater.inflate(R.layout.item_no_data,parent,false));
        }
        if(viewType==TYPE_WOOL_RAIDERS){
            //羊毛攻略
            return new WoolRaidersHolder(inflater.inflate(R.layout.item_wool_raiders,parent,false));
        }
        if(viewType== TYPE_CREDIT_RAIDERS||viewType==TYPE_UPPER_QUOTA_RAIDERS||viewType==TYPE_TOPIC_RAIDERS){
            //办卡、提额、专题
            return new FeaturedRaidersHolder(inflater.inflate(R.layout.item_featured_raiders,parent,false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof NoDataHolder){
            return;
        }
        if(holder instanceof WoolRaidersHolder){
            RaidersList.Raiders raiders=data.get(position);
            int modeSpace=position%3;
            WoolRaidersHolder raidersHolder= (WoolRaidersHolder) holder;
            switch (modeSpace){
                case 0:
                    raidersHolder.tvDate.setBackgroundResource(R.drawable.shape_corner_1dp_solid_ffbd18);
                    break;
                case 1:
                    raidersHolder.tvDate.setBackgroundResource(R.drawable.shape_corner_1dp_solid_fa864b);
                    break;
                case 2:
                    raidersHolder.tvDate.setBackgroundResource(R.drawable.shape_corner_1dp_solid_7dd66e);
                    break;
            }
            String day=raiders.getTopical();
            raidersHolder.tvDate.setText(day);
            if("今天".equals(day)){
                raidersHolder.tvDate.setBackgroundResource(R.drawable.shape_corner_1dp_solid_f72d48);
                raidersHolder.ivPromise.setVisibility(View.VISIBLE);
            }else{
                raidersHolder.ivPromise.setVisibility(View.INVISIBLE);
            }
            raidersHolder.tvTitle.setText(raiders.getName());
            raidersHolder.tvContent.setText(raiders.getMarketing_one());
            raidersHolder.tvContentBottom.setText(raiders.getMarketing_two());
            raidersHolder.tvViewCount.setText(String.valueOf(raiders.getNumber()));
            ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL+raiders.getPicture_path(),
                    raidersHolder.image,
                    R.mipmap.img_default_rectangle);
        }

        if(holder instanceof FeaturedRaidersHolder){
            RaidersList.Raiders raiders=data.get(position);
            FeaturedRaidersHolder raidersHolder= (FeaturedRaidersHolder) holder;
            raidersHolder.tvTitle.setText(raiders.getName());
            raidersHolder.tvContent.setText(raiders.getMarketing_one());
            raidersHolder.tvViewCount.setText(String.valueOf(raiders.getNumber()));
            ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL+raiders.getPicture_path(),
                    raidersHolder.image,
                    R.mipmap.img_default_rectangle);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(clickedCallback!=null){
                    clickedCallback.onRecycleItemClicked(position);
                }
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        if(data==null||data.size()==0){
            return TYPE_NO_DATA;
        }
        return raidersType;
    }

    @Override
    public int getItemCount() {
        return (data==null||data.size()==0)?1:data.size();
    }

    private class WoolRaidersHolder extends RecyclerView.ViewHolder{
        private ImageView image;
        private TextView tvTitle;
        private TextView tvContent;
        private TextView tvContentBottom;
        private ImageView ivPromise;
        private TextView tvDate;
        private TextView tvViewCount;
        private WoolRaidersHolder(View itemView) {
            super(itemView);
            image= (ImageView) itemView.findViewById(R.id.image);
            tvTitle= (TextView) itemView.findViewById(R.id.tv_title);
            tvContent= (TextView) itemView.findViewById(R.id.tv_content);
            tvContentBottom= (TextView) itemView.findViewById(R.id.tv_content_bottom);
            ivPromise= (ImageView) itemView.findViewById(R.id.iv_promise);
            tvDate= (TextView) itemView.findViewById(R.id.tv_date);
            tvViewCount= (TextView) itemView.findViewById(R.id.tv_view_count);
        }
    }

    private class FeaturedRaidersHolder extends RecyclerView.ViewHolder{
        private ImageView image;
        private TextView tvTitle;
        private TextView tvContent;
        private TextView tvViewCount;
        private FeaturedRaidersHolder(View itemView) {
            super(itemView);
            image= (ImageView) itemView.findViewById(R.id.image);
            tvTitle= (TextView) itemView.findViewById(R.id.tv_title);
            tvContent= (TextView) itemView.findViewById(R.id.tv_content);
            tvViewCount= (TextView) itemView.findViewById(R.id.tv_view_count);
        }
    }

    private class NoDataHolder extends RecyclerView.ViewHolder{
        private NoDataHolder(View itemView) {
            super(itemView);
            itemView.setVisibility(View.VISIBLE);
        }
    }
}
